package cn.dxh.stagefinal.utils;

import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.dao.LoginDao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 2019/12/11 11:25
 *
 * @author Wenhao Lee
 * 心向远方，不负厚望。
 */
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private LoginDao loginDao;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("登录认证开始");
        String loginName = (String) authenticationToken.getPrincipal();
        Employee employee = loginDao.login(loginName);
        if (employee == null) {
            System.out.println("登录认证结束 -- 用户找不到");
            return null;
        }
        System.out.println("登录认证结束 -- 用户找得到");
        return new SimpleAuthenticationInfo(employee, employee.getePassword(), ByteSource.Util.bytes(employee.getSalt()), this.getName());
    }
}
